<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <title>Boost 搜索引擎</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html,
        body {
            height: 100%;
            font-family: Arial, sans-serif;
        }

        body {
            padding: 20px;
            background-color: #f8f9fa;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            margin-top: 50px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #4e6ef2;
            font-size: 48px;
            margin-bottom: 15px;
        }

        .container .search {
            width: 100%;
            height: 52px;
            display: flex;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            border-radius: 26px;
            overflow: hidden;
        }

        .container .search input {
            flex: 1;
            height: 100%;
            border: none;
            padding: 0 20px;
            font-size: 16px;
            outline: none;
        }

        .container .search input:focus {
            color: #333;
        }

        .container .search button {
            width: 150px;
            height: 100%;
            background-color: #4e6ef2;
            color: #FFF;
            font-size: 16px;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .container .search button:hover {
            background-color: #3a5edb;
        }

        .loading {
            text-align: center;
            padding: 20px;
            display: none;
        }

        .container .result {
            width: 100%;
            margin-top: 30px;
        }

        .container .result .item {
            margin-top: 20px;
            padding: 15px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            transition: box-shadow 0.3s;
        }

        .container .result .item:hover {
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
        }

        .container .result .item a {
            display: block;
            text-decoration: none;
            font-size: 20px;
            color: #1a0dab;
            margin-bottom: 5px;
        }

        .container .result .item a:hover {
            text-decoration: underline;
        }

        .container .result .item p {
            font-size: 14px;
            color: #545454;
            line-height: 1.5;
            margin-bottom: 5px;
        }

        .container .result .item i {
            display: block;
            font-style: normal;
            color: #006621;
            font-size: 14px;
        }

        .no-results {
            text-align: center;
            padding: 50px 0;
            color: #666;
            display: none;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                width: 100%;
                padding: 0 15px;
            }

            .header h1 {
                font-size: 36px;
            }

            .container .search button {
                width: 100px;
                font-size: 14px;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <h1>Boost 搜索</h1>
        </div>
        <div class="search">
            <input type="text" placeholder="请输入搜索关键字" id="searchInput">
            <button onclick="Search()"><i class="fa fa-search"></i> 搜索一下</button>
        </div>

        <div class="loading">
            <i class="fa fa-circle-o-notch fa-spin"></i> 正在搜索中...
        </div>

        <div class="no-results">
            <i class="fa fa-search-minus"></i> 没有找到相关结果
        </div>

        <div class="result">
            <!-- 动态生成网页内容 -->
        </div>
    </div>
    <script>
        // 使用jQuery的方式绑定键盘事件，更可靠
        $(document).ready(function() {
            // 为搜索输入框绑定键盘按下事件
            $("#searchInput").keydown(function(event) {
                // 检查是否按下的是Enter键（keyCode 13）
                if (event.keyCode === 13) {
                    // 阻止默认行为（避免表单提交等默认操作）
                    event.preventDefault();
                    // 执行搜索
                    Search();
                }
            });
        });

        function Search() {
            let query = $("#searchInput").val();
            console.log("query = " + query);

            // 显示加载状态
            $(".loading").show();
            // 隐藏无结果提示和之前的结果
            $(".no-results").hide();
            $(".result").empty();

            // 如果查询为空，不执行搜索
            if (!query.trim()) {
                $(".loading").hide();
                return;
            }

            $.ajax({
                type: "GET",
                url: "/s?word=" + encodeURIComponent(query),
                success: function (data) {
                    console.log(data);
                    // 隐藏加载状态
                    $(".loading").hide();
                    
                    // 如果没有结果，显示无结果提示
                    if (data.length === 0) {
                        $(".no-results").show();
                    } else {
                        BuildHtml(data);
                    }
                },
                error: function() {
                    // 错误处理
                    $(".loading").hide();
                    $(".no-results").show();
                }
            });
        }

        function BuildHtml(data) {
            let result_lable = $(".container .result");
            result_lable.empty();

            for (let elem of data) {
                let a_lable = $("<a>", {
                    text: elem.title,
                    href: elem.url,
                    target: "_blank"
                });
                let p_lable = $("<p>", {
                    text: elem.desc
                });
                let i_lable = $("<i>", {
                    text: elem.url
                });
                let div_lable = $("<div>", {
                    class: "item"
                });
                a_lable.appendTo(div_lable);
                p_lable.appendTo(div_lable);
                i_lable.appendTo(div_lable);
                div_lable.appendTo(result_lable);
            }
        }
    </script>
</body>

</html>
